package com.lebang.controller.buss;

import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.entity.sys.Custom;
import com.lebang.entity.task.TaskComments;
import com.lebang.example.CustomExample;
import com.lebang.example.TaskCommentsExample;
import com.lebang.service.buss.CustomService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * 描述：客户管理控制层
 *
 * @author HWQ
 * @date Mon Apr 22 11:09:43 CST 2019
 */
@Controller
@RequestMapping("/custom")

public class CustomController {

    @Autowired
    private CustomService customService;
    @Login
    @GetMapping("/list")
    public String list() {
        return "/custom/tbcustom-list";
    }

    @Login
    @PostMapping("/data")
    @ResponseBody
    @DataSource("commonDB")
    public HashMap data(Long page, Integer limit){
        HashMap map = new HashMap(1);
        CustomExample example = new CustomExample();
        CustomExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("create_time desc");
        c.andHasDeletedEqualTo(0);

        map.put("count", customService.count(example));
        map.put("code", 0);
        List<Custom> masters = customService.selectAll(example);
        map.put("data", masters);
        return map;

    }

    /**新增修改保存*/
    @PostMapping("/edit")
    @Login
    @ResponseBody
    @DataSource("commonDB")
    public JsonData edit(Custom po, HttpSession session) {
        if (po.getId() == null) {
            po.setCustomId(UUIDUtil.M());
            po.setCreateUserid((String) session.getAttribute("userid"));
            return customService.insert(po);
        } else {
            return customService.update(po);
        }
    }

    /**新增修改跳转*/
    @GetMapping("/edit")
    @Login
    @DataSource("commonDB")
    public String edit(@RequestParam(name = "id", required = false) Integer id,
                       Model model, Custom custom) {
        if (!"".equals(id) && null != id) {
            model.addAttribute("item", customService.selectByPrimaryKey(id));
        } else {
            model.addAttribute("item", custom);
        }
        return "/custom/tbcustom-edit";
    }


    @Login
    @RequestMapping(value = ("/delete/data"), method = RequestMethod.POST)
    @ResponseBody
    @DataSource("commonDB")
    public JsonData deletedData( Integer ids) {
        customService.deletedData(ids);
            return Callback.success("200", "操作成功", "");
    }

}